package ducas.getprice.demo;

import com.dukascopy.api.*;
import ducas.getprice.db.DataAccessObject;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class MA_Play implements IStrategy {
    private IEngine engine = null;
    private IIndicators indicators = null;
    private int tagCounter = 0;
    private double[] ma1 = new double[Instrument.values().length];
    private IConsole console;
    @Override
    public void onStart(IContext context) throws JFException {
        engine = context.getEngine();
        indicators = context.getIndicators();
        this.console = context.getConsole();
        console.getOut().println("Started");
    }
    @Override
    public void onStop() throws JFException {
        for (IOrder order : engine.getOrders()) {
            order.close();
        }
        console.getOut().println("Stopped");
    }
    //Added by nothing
    private String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        java.util.Date date = new java.util.Date();
        return dateFormat.format(date);
    }
    @Override
    public void onTick(Instrument instrument, ITick tick) throws JFException {
    	System.out.println("=BID/ASK=: \t" + "=STOCKS="           +"\t"+"=PRICE="      +"\t\t"+"===TIME===");
    	System.out.println("BID Price: \t" + instrument.toString()+"\t\t"+tick.getBid()+"\t\t"+getDateTime());
    	System.out.println("ASK Price: \t" + instrument.toString()+"\t\t"+tick.getAsk()+"\t\t"+getDateTime());
        String sSQL1 = "INSERT INTO get_prices_backup(INSTRUMENTS, BID, ASK, SERVER_TIME, LOCAL_TIME) VALUES("
                + "'"
                + instrument.toString()
                +"', "
                + tick.getBid()
                + ", "
                + tick.getAsk()
                + ", "
                + tick.getTime()
                + ", '"
                + this.getDateTime().toString()
                + "'"
                + ")";
        
        String sSQL2="update get_prices_online set "
                + "INSTRUMENTS='"+instrument.toString()+"'"
                + ", BID=" + tick.getBid()
                + ", ASK=" + tick.getAsk()
                + ", SERVER_TIME=" + tick.getTime()
                + ", LOCAL_TIME='" + this.getDateTime().toString()
                + "'"
                ;
        try {
            DataAccessObject DAO = new DataAccessObject();
            Statement st = DAO.getConnection().createStatement();
            st.execute(sSQL1);
            st.execute(sSQL2);
        } catch (SQLException s){
            System.out.println("SQL statement is not executed: " + s.getMessage());
        }
    }
    @Override
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) {
    }
    //count open positions
    protected int positionsTotal(Instrument instrument) throws JFException {
        int counter = 0;
        for (IOrder order : engine.getOrders(instrument)) {
            if (order.getState() == IOrder.State.FILLED) {
                counter++;
            }
        }
        return counter;
    }
    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
        label = label.substring(0, 2) + label.substring(3, 5);
        label = label + (tagCounter++);
        label = label.toLowerCase();
        return label;
    }
    @Override
    public void onMessage(IMessage message) throws JFException {
    }
    @Override
    public void onAccount(IAccount account) throws JFException {
    }
}